<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	 public function index()
	{
		$data['main_view']='admin/main';
		$this->load->view('login',$data);
	}
	
	
	function __construct() {
        parent::__construct();	
		
		$this->load->model('login_model');
		
	}


	
	function doLogin(){
		$username      = $this->input->post('username');
        $password   = $this->input->post('password');
		
		$this->form_validation->set_rules('username','Username','required');
		$this->form_validation->set_rules('password','Password','required');
		$this->form_validation->set_message('required','%s harus diisi');
		if($this->form_validation->run()==false){
			$this->index();
		}else{
        	$pass   = md5($password);
        	$cek=$this->login_model->doLogin($username,$pass);
        	if($cek!=false){
				if($this->login_model->cekStatus($cek['id'])){
           			$data = array (
                		'isLogin' => 'yes',
						'id'=>$cek['id'],
                		'user_id' => $cek['username'],
						'foto' => $cek['foto'],
                		'level' => $cek['user_type']
           			);
            		$this->session->set_userdata($data);
            		redirect('cmstenet');
				}else{
					$this->session->set_flashdata('error_login', 'Username ini sudah tidak Aktif !');
            	redirect('login');
				}
        	}else{
				$this->session->set_flashdata('error_login', 'Username atau password anda salah !');
            	redirect('login');
        	}
		}
	}
	
	function logOut(){
		$this->session->unset_userdata('isLogin');
		$this->session->unset_userdata('user_id');
		$this->session->unset_userdata('level');
		$this->session->sess_destroy();
		$this->session->set_flashdata('error_login', 'Anda Sudah Log Out!');
		redirect('login');
	}
	
	function logout_pelanggan(){
		$this->session->unset_userdata('isLogin');
		$this->session->unset_userdata('id_pelanggan');
		$this->session->sess_destroy();
		redirect('home');
	}
	
	function logOutPemilik(){
		$this->session->unset_userdata('isLogin');
		$this->session->unset_userdata('id_pemilik');
		$this->session->unset_userdata('nama_pemilik');
		$this->session->sess_destroy();
		$this->session->set_flashdata('error_login', 'Anda Sudah Log Out!');
		redirect('login/login_pemilik');
	}
	
}